package cn.jingyuan.swan.system.domain.sys.dao.mysql;

import cn.jingyuan.swan.cloud.dao.DaoException;
import cn.jingyuan.swan.cloud.dao.mybatisplus.AbstractMyBatisPlusCrudDao;
import cn.jingyuan.swan.system.domain.sys.dao.RoleUserDao;
import cn.jingyuan.swan.system.domain.sys.entity.RoleUser;
import cn.jingyuan.swan.system.domain.sys.mapper.RoleUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class RoleUserDaoMySql extends AbstractMyBatisPlusCrudDao<RoleUser, Long> implements RoleUserDao {

    @Resource
    RoleUserMapper mapper;

    @Override
    public boolean isExist(Long userId, Long roleId) {
        LambdaQueryWrapper<RoleUser> wrapper = this.queryWrapper().eq(RoleUser::getUserId, userId).eq(RoleUser::getRoleId, roleId);


        try {
            Integer row = mapper.selectCount(wrapper);
            return row > 0;
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public int countByUserId(Long userId) {
        LambdaQueryWrapper<RoleUser> wrapper = this.queryWrapper().eq(RoleUser::getUserId, userId);

        try {
            return mapper.selectCount(wrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public int deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<RoleUser> wrapper = this.queryWrapper().eq(RoleUser::getRoleId, roleId);

        try {
            return mapper.delete(wrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public int deleteByUserId(Long userId) {
        LambdaQueryWrapper<RoleUser> wrapper = this.queryWrapper().eq(RoleUser::getUserId, userId);

        try {
            return mapper.delete(wrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public List<RoleUser> listByRoleId(Long roleId) {
        LambdaQueryWrapper<RoleUser> wrapper = this.queryWrapper().eq(RoleUser::getRoleId, roleId);

        try {
            return mapper.selectList(wrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public int delete(Long id) {
        throw DAO_EX_NONSUPPORT;
    }

    @Override
    public int deletes(List<Long> ids) {
        throw DAO_EX_NONSUPPORT;
    }

    @Override
    public int update(RoleUser entity) {
        throw DAO_EX_NONSUPPORT;
    }

    @Override
    public int updates(List<RoleUser> entities) {
        throw DAO_EX_NONSUPPORT;
    }

    @Override
    public RoleUser get(Long id) {
        throw DAO_EX_NONSUPPORT;
    }

    @Override
    public List<RoleUser> gets(List<Long> ids) {
        throw DAO_EX_NONSUPPORT;
    }

    @Override
    public BaseMapper<RoleUser> getMapper() {
        return this.mapper;
    }

}
